<?php


namespace App\Services;

use App\Common\Helper;
use App\Models\Admin;
use App\Models\Menu;
use App\Modules\Live\Models\ShimmerLiveshopMenu;
use App\User;

/**
 * 菜单服务
 * Class MenuService
 * @package App\Services
 */
class MenuService extends BaseService
{
    /**
     * 设置左边菜单栏
     */
    public static function setPageMenu()
    {
        $leftMenuData = self::getLeftMenuLists();
        \View::share('leftMenuData', $leftMenuData);
    }

    /**
     * @return array
     * 获取左侧菜单
     */
    public static function getLeftMenuLists(){
        if(\Auth::guard('admin')->user()->uid == 1){
            $menu = Menu::orderBy('sort','DESC')
                ->orderBy('id','desc')->where(['status'=>1])->where('type', 'admin')->get();
        }else{
            $menu = Admin::join('user_roles', 'user_roles.user_id', 'admins.uid')
                ->join('permission', 'permissions.role_id', 'user_roles.role_id')
                ->join('menus','menus.id', 'permissions.menu_id')
                ->select(['menus.*'])
                ->where('users.uid', \Auth::guard('admin')->user()->uid)
                ->where('menus.type', 'admin')
                ->get();
        }
        $data = array();
        if ($menu){
            foreach($menu as $key=>$item){
                $data[$item->id] = [
                    'id'=>$item->id,
                    'pid'=>$item->pid,
                    'name'=>$item->name,
                    'route'=>$item->route,
                    'active'=> \request()->route()->named($item->route),
                    'ico'=>$item->ico,
                    'url'=>(\Route::has($item->route)==false)?'javascript:;':route($item->route),
                ];
            }
            if ($data){
                return Helper::generateTree($data);
            }
        }
        return $data;
    }

}